package net.entelijan.cobean.examples.showcase.tree;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.Icon;
import javax.swing.JOptionPane;

import net.entelijan.cobean.core.ColumnDesc;
import net.entelijan.cobean.core.ISingleSelectionCobean;
import net.entelijan.cobean.data.literature.ILitService;
import net.entelijan.cobean.tree.IIconMapper;
import net.entelijan.cobean.tree.RecursiveTreeInitializer;
import net.entelijan.cobean.util.CobeanUtil;
import net.entelijan.scf.IShowcase;

public class RecursiveTreeShowcase implements IShowcase<RecursiveTreePanel> {

	private ILitService service = null;

	public RecursiveTreeShowcase() {
		super();
	}

	@Override
	public void init(final RecursiveTreePanel panel) {

		// Create an initializer
		RecursiveTreeInitializer<File> ini = new RecursiveTreeInitializer<File>();
		ini.setCoTree(panel.getTree());
		ini.setCoCollapseAllButton(panel.getCollapseAllButton());
		ini.setChildrenMethodName("listFiles");
		ini.setColumnDescs(ColumnDesc.createPropertyOnlyColumnDescs("name",
				"parentFile.name"));
		ini.setIconMapper(new IIconMapper<File>() {

			@Override
			public Icon getIcon(File model) {
				Icon re = null;
				if (model.isDirectory()) {
					re = CobeanUtil.loadIcon("icons/sign_warning.png");
				} else if (model.getName().endsWith("xml")) {
					re = CobeanUtil.loadIcon("icons/sign_info.png");
				} else {
					re = null;
				}
				return re;
			}
		});

		// Init the components and receive your component bean.
		// From now on the component bean is the only interface to the
		// 'real' components.
		final ISingleSelectionCobean<File> cobean = ini.initialize();
		cobean.setValue(new File(System.getProperty("user.home")));

		// Now the table displays some data

		panel.getShowSelectionButton().addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(panel, cobean.getSelectedValue());
			}

		});

	}

	public ILitService getService() {
		return service;
	}

	public void setService(ILitService service) {
		this.service = service;
	}

}
